package com.dhp.bkOjjudgeservice.rabbitMQ;


import com.dhp.constant.MQConstant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * rabbitMQ的初始化
 */
@Component
@Slf4j
public class mqInit {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public static void doInit(){
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        try {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            channel.exchangeDeclare(MQConstant.EXCHANGE_NAME , "direct" , true , false , null);
            channel.queueDeclare(MQConstant.QUEUE_NAME , true , false , false , null);
            channel.queueBind(MQConstant.QUEUE_NAME , MQConstant.EXCHANGE_NAME  , MQConstant.ROUTING_KEY);
            log.info("消息队列初始化成功");
        } catch (Exception e) {
            log.info("消息队列启动失败");
            e.printStackTrace();
        }
    }
}
